package com.kanshu.ksgb.fastread.doudou.module.bookcity.view;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.kanshu.ksgb.fastread.doudou.R;
import com.kanshu.ksgb.fastread.doudou.app.constants.Constants;
import com.kanshu.ksgb.fastread.doudou.common.business.ad.presenter.AdPresenter;
import com.kanshu.ksgb.fastread.doudou.common.urlrouter.UrlRouter;
import com.kanshu.ksgb.fastread.doudou.common.util.DisplayUtils;
import com.kanshu.ksgb.fastread.doudou.common.util.Utils;
import com.kanshu.ksgb.fastread.doudou.module.bookcity.bean.ItemRefreshBean;
import com.kanshu.ksgb.fastread.doudou.module.bookcity.bean.SelectedBean;
import com.kanshu.ksgb.fastread.doudou.module.bookcity.presenter.BookCityPresenter;
import com.kanshu.ksgb.fastread.doudou.module.bookcity.utils.BookCityUtils;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.subjects.PublishSubject;
import io.reactivex.subjects.Subject;

import static com.kanshu.ksgb.fastread.doudou.base.baseui.SwipeBackActivity.ACTION_DESTORY;

/**
 * Created by wcy on 18/5/15.
 */

public class AdSelectedSevenLayout extends LinearLayout implements IRefresh<SelectedBean> {


    @BindView(R.id.label)
    TextView mLabel;
    ItemRefreshBean mItemRefreshBean;
    @BindView(R.id.tag_container)
    RelativeLayout mTagContainer;
    @BindView(R.id.change_tip)
    ImageView mChangeTip;
    public Subject<Integer> lifeCyclerSubject = PublishSubject.create();
    BookCityPresenter presenter;
    @BindView(R.id.divider)
    LinearLayout mDivider;
    HashMap<String, Boolean> mShowNormalDatas = new HashMap<>();
    String mType;

    public AdSelectedSevenLayout(Context context) {
        super(context);
        init();
    }

    public AdSelectedSevenLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public AdSelectedSevenLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        presenter = new BookCityPresenter(lifeCyclerSubject);
        setOrientation(VERTICAL);
        View view = LayoutInflater.from(getContext()).inflate(R.layout.item_ad_selected_seven_layout, this);
        ButterKnife.bind(view);
        DisplayUtils.gone(mDivider);
    }

    @Override
    public void refresh(SelectedBean selectedBean, Object object) {
        mItemRefreshBean = (ItemRefreshBean) object;
        if (selectedBean == null || mItemRefreshBean == null) {
            return;
        }
        mType = mItemRefreshBean.type;
        BookCityUtils.rotateCancel(mChangeTip);
        mLabel.setText(selectedBean.name);
        mLabel.setCompoundDrawablesWithIntrinsicBounds(mItemRefreshBean.selectionIcon, 0, 0, 0);
        if (selectedBean != null && !Utils.isEmptyList(selectedBean.list)) {
            if (selectedBean.list.size() > 5) {
                selectedBean.list = selectedBean.list.subList(0, 5);
            }
            if (mShowNormalDatas.get(mType) != null
                    && mShowNormalDatas.get(mType)) {
                return;
            }
            for (int i = 0; i < selectedBean.list.size(); i++) {
                ((TextView) mTagContainer.getChildAt(i)).setText(selectedBean.list.get(i).title);
                final int pos = i;
                mTagContainer.getChildAt(i).setOnClickListener(v -> {
                    UrlRouter.from(getContext()).jump(selectedBean.list.get(pos).jump_url);
                    BookCityUtils.takeIf(mItemRefreshBean, Constants.BookCityModuleType.TYPE_SElECTED_REMENBIAOQIAN, () -> {

                        AdPresenter.pvuvStatics(pos,
                                R.string.SCTUIJIAN10_POSITION1,
                                R.string.SCTUIJIAN10_POSITION4,
                                R.string.SCTUIJIAN10_POSITION3,
                                R.string.SCTUIJIAN10_POSITION2,
                                R.string.SCTUIJIAN10_POSITION5);
                    });
                });
            }
            mShowNormalDatas.put(mType, true);
        }
    }

    @Override
    public void onError(int code, String desc) {
        BookCityUtils.rotateCancel(mChangeTip);
    }

    @Override
    public void reset() {
        if (mShowNormalDatas != null) {
            mShowNormalDatas.clear();
        }
    }

    @OnClick(R.id.change_container)
    public void onViewClicked() {
        mShowNormalDatas.put(mType, false);
        BookCityUtils.rotate(mChangeTip);
        BookCityUtils.getHotLabel(mItemRefreshBean, this, null, presenter);
        BookCityUtils.takeIf(mItemRefreshBean, Constants.BookCityModuleType.TYPE_SElECTED_REMENBIAOQIAN, () -> {
            AdPresenter.pvuvStatics(R.string.SCTUIJIAN10_HUANYIHUAN);
        });
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        lifeCyclerSubject.onNext(ACTION_DESTORY);
    }
}
